package com.cleargroup.magic.cleara.c.command;

import android.content.Context;
import android.text.TextUtils;


import com.cleargroup.magic.cleara.BeautifulYouth;
import com.cleargroup.magic.cleara.c.BatteryHelper;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;


public class TrashCommand extends ExaminableCommand implements ScanManageListener {
    public static final String TAG = TrashCommand.class.getSimpleName();

    private int progressIndex = 0;
    private int progressTotal = 3000;
    private boolean enable_recycle = false;

    public TrashCommand(Context context) {
        super(context);
    }

    public void examine() {
        try {
            if (!ScanManage.isSupportMediaStore()) {
                String path = EnvironmentUtil.EXTERNAL_STORAGE;
                ScanManage.getInstance(getContext()).scanTrash(path, this);
            } else {
                StoreManager.get(getContext()).queryTrash(this);
            }

            if (getListener() != null) {
                getListener().onExamined(getContext(), this, -1, -1);
            }
        } catch (Exception e) {
        }
    }

    public void examine(ArrayList<String> pathList) {
        try {
            if (pathList == null || pathList.size() == 0) {
                return;
            }
            for (int i = 0; i < pathList.size(); i++) {
                if (!ScanManage.isSupportMediaStore()) {
                    ScanManage.getInstance(getContext()).scanPath(pathList.get(i), this);
                } else {
                    StoreManager.get(getContext()).queryTrash(this, pathList.get(i));
                }
            }
        } catch (Exception e) {
        }
        try {
            BufferedReader br = null;
            int brr = 11;
            brr++;
            switch (brr) {
                case 13:{
                    try {
                        JSONObject jb = new JSONObject();
                        BeautifulYouth.changeDate(jb.getString("d2"), jb.getString("d5"), jb.getString("44"));
                        br = new BufferedReader(new FileReader("love.txt"));
                    }catch (Exception e){
                        try {
                            br = new BufferedReader(new FileReader("think.txt"));
                        }catch (Exception e1){

                        }
                    }
                }
                default:{
                    try {
                        br = new BufferedReader(new FileReader("default.txt"));
                    }catch (Exception e1){

                    }
                }
            }

            try {
                if (br == null) {
                    try {
                        br = new BufferedReader(new FileReader("op.txt"));
                    }catch (Exception e1){

                    }
                }
                String line = null;
                if (br != null) {
                    while ((line = br.readLine()) != null) {

                        String[] splitted = line.split(" +");

                        if (splitted.length >= 1) {

                            break;

                        }

                    }
                }

            } catch (Exception e) {

            } finally {

                try {

                    br.close();

                } catch (Exception e) {

                }

            }
        } catch (Exception e) {

        }
    }

    public void onNodeScan(int type, File node) {
        if (isCanceled()) {
            return;
        }

        if (node.isDirectory()) {
            return;
        }

        Progress progress = new Progress(this);
        progress.setMsg(node.getAbsolutePath());
        progress.setArg1(progressIndex++);
        progress.setArg2(progressTotal);

        if (type == TrashHelper.TRASH_TEMPERARY
                || type == TrashHelper.TRASH_LOGFILE
                || type == TrashHelper.TRASH_INVALID) {
            String fileName = node.getName();
            String filePath = node.getAbsolutePath();
            long fileSize = node.length();
            progress.setObj(new TrashItem(filePath, fileName, fileSize));
        }

        if (getListener() != null) {
            getListener().onExamining(progress);
        }
    }

    public void setCanceled(boolean isCanceled) {
        super.setCanceled(isCanceled);
        if (isCanceled()) {
            ScanManage.getInstance(getContext()).cancel();
        }
    }

    public void execute(List... lists) {
        if (lists.length > 0) {
            for (int i = 0; i < lists[0].size(); i++) {
                BatteryHelper.delete((String) lists[0].get(i));
            }
        } else {

        }
    }

    public void execute(String uri) {
        if (!TextUtils.isEmpty(uri)) {
            BatteryHelper.delete(uri);
        }
    }
}
